package com.health.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.health.system.annotation.Log;
import com.health.system.common.result.Result;
import com.health.system.common.validated.groups.UpdateGroup;
import com.health.system.entity.Role;
import com.health.system.service.RoleService;
import com.health.system.vo.AdminRoleVo;
import com.health.system.vo.RoleListPageSearchVo;
import com.health.system.vo.UpdateStatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

import static com.health.system.constant.LogTypeConstant.*;


@Validated
@Api(tags = "角色管理")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Log(SELECT)
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "current", value = "当前页", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "size", value = "每页显示数量", dataTypeClass = Long.class)
            }
    )
    @ApiOperation(value = "角色列表")
    @GetMapping("/admin/roles/listPage")
    public Result<IPage<Role>> listPage(@RequestParam(value = "current", defaultValue = "1") Long current,
                                        @RequestParam(value = "size", defaultValue = "10") Long size,
                                        RoleListPageSearchVo condition) {
        IPage<Role> result = roleService.selectListPage(current, size ,condition);
        return Result.ok(result);
    }

    @Log(INSERT)
    @ApiOperation(value = "角色新增")
    @PostMapping("/admin/roles/insert")
    public Result<String> insert(@Valid @RequestBody AdminRoleVo vo) {
        roleService.insert(vo);
        return Result.ok();
    }

    @Log(UPDATE)
    @Validated(UpdateGroup.class)
    @ApiOperation(value = "菜单分配")
    @PostMapping("/admin/roles/alloc/menu")
    public Result<String> allocMenu(@Valid @RequestBody AdminRoleVo vo) {
        roleService.allocMenu(vo);
        return Result.ok();
    }

    @Log(UPDATE)
    @Validated(UpdateGroup.class)
    @ApiOperation(value = "资源分配")
    @PostMapping("/admin/roles/alloc/resource")
    public Result<String> allocResource(@Valid @RequestBody AdminRoleVo vo) {
        roleService.allocResource(vo);
        return Result.ok();
    }

    @Log(DELETE)
    @ApiImplicitParam(name = "ids", value = "角色id", required = true, dataTypeClass = List.class)
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/admin/roles/delete")
    public Result<String> batchDelete(@NotEmpty(message = "角色id不能为空") @RequestBody List<Long> ids) {
        roleService.batchDelete(ids);
        return Result.ok();
    }

    @Log(UPDATE)
    @ApiOperation(value = "角色状态切换")
    @PostMapping("/admin/roles/update/status")
    public Result<String> updateStatus(@RequestBody UpdateStatusVo vo) {
        roleService.updateStatus(vo);
        return Result.ok();
    }

}
